package com.example.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * OssUtil 工具类
 */
@Slf4j
@Component
public class OssUtil {
    @Value("${oss.bucket}")
    private String OSS_BUCKET;

    @Value("${oss.bucketUrl}")
    private String OSS_BUCKET_URL;

    @Resource
    private OSS ossClient;

    /**
     * 上传图片
     */
    public String uploadImage(@NonNull MultipartFile file, @NonNull String type) throws IOException{
        //对上传的照片生成特有的UUID标识，组成文件名
        String originalFilename = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = System.currentTimeMillis() + UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + ext;
        
        String filePathName = "";
        if(type.equals("avatarImg")||type.equals("Photo")){
            String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
            filePathName = currentDate +"Images/" + type + "/" +fileName;
        }
        
        try {
            ossClient.putObject(
                    OSS_BUCKET,
                    filePathName,
                    file.getInputStream()
            );
        }catch (OSSException oe) {
            log.error("OSS出错了:" + oe.getMessage());
        }catch (ClientException ce) {
            log.error("OSS出错了:" + ce.getMessage());
        }
        return OSS_BUCKET_URL + filePathName;
    }

    /**
     * 上传视频
     */
    public String uploadVideo(@NonNull MultipartFile file) throws IOException{
        //基础操作
        String originalVideoName = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(originalVideoName);
        String uuid = System.currentTimeMillis() + UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + ext;

        //打上日期归类
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).replace("-","");
        String filePathName = date+"/video/" + fileName;
        try{
            ossClient.putObject(
                    OSS_BUCKET,
                    filePathName,
                    file.getInputStream()
            );
        }catch (OSSException oe) {
            log.error("OSS出错了:" + oe.getErrorMessage());
            throw oe;
        } catch (ClientException ce) {
            log.error("OSS连接出错了:" + ce.getMessage());
            throw ce;
        }
        //返回视频Url
        return OSS_BUCKET_URL + filePathName;
    }

    /**
     * OSS删除文件
     * @param filename
     */
    public void deleteFiles(@NonNull String filename) {
        try {
            if (filename.isEmpty()) {
                log.warn("文件名为空，拒绝删除操作");
                return;
            }

            ossClient.deleteObject(OSS_BUCKET, filename);
            log.info("成功删除文件: " + filename);
        } catch (OSSException oe) {
            log.error("删除OSS文件出错: " + oe.getErrorMessage());
        } catch (ClientException ce) {
            log.error("OSS连接出错: " + ce.getMessage());
        }
    }


}
